<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-11
 * Time: 下午9:27
 * To change this template use File | Settings | File Templates.
 */
class center extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        if (!$this->isLogin()) {
            $this->load->helper('url');
            redirect("/user/login");
            return ;
        }
    }

    /**
     * 用户中心首页
     */
    public function index()
    {
        $this->load->model('user/Model_User', 'user');
        $uInfo = $this->user->getUserInfoByPassport($this->uInfo['passport']);

        $this->load->model('Model_Weiqi_User', 'weiqi');
        //$data = $this->weiqi->NewUserInfo($this->uInfo['passport']);
        $data = $this->weiqi->NewUserInfo($this->uInfo['passport']);

        //$data = $this->weiqi->NewUserInfo('');
        $info = $this->weiqi->formatData($data);

        //echo '<pre>';print_r($info);exit;

        $this->load->view('user/index', array('current' => 'index', 'navigation' => 'center', 'user_info' => $uInfo, 'data' => isset ($info['z:row']) ? $info['z:row'] : array()));
    }

    /**
     * 我的资料
     */
    public function profile()
    {
        $this->load->model('user/Model_User', 'user');
        $uInfo = $this->user->getUserInfoByPassport($this->uInfo['passport']);

        $this->load->view('user/profile', array('current' => 'profile', 'navigation' => 'center', 'user_info' => $uInfo));
    }

    /**
     * 修改资料
     */
    public function modifyProfile()
    {
        $province = array( '北京市', '天津市', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省', '黑龙江省', '上海市', '江苏省', '浙江省', '安徽省', '福建省',
            '江西省', '山东省', '河南省', '湖北省', '湖南省', '广东省', '广西壮族自治区', '海南省', '重庆市', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省',
            '甘肃省', '青海省', '宁夏回族自治区', '新疆维吾尔自治区', '香港特别行政区', '澳门特别行政区', '台湾省', );

        $this->load->view('user/modify_profile', array('current' => 'profile', 'navigation' => 'center', 'user_info' => $this->uInfo, 'province' => $province));
    }

    public function saveProfile()
    {
        $idCard = trim($this->input->get_post('id_card'));
        $sex = trim($this->input->get_post('sex'));
        $birthday = trim($this->input->get_post('birthday'));
        $address = trim($this->input->get_post('address'));
        $phone = trim($this->input->get_post('phone'));
        $email = trim($this->input->get_post('email'));

        $this->uInfo['passport'];

        $data = array(
            'id_card' => $idCard,
            'sex' => $sex,
            'birthday' => $birthday,
            'address' => $address,
            'phone' => $phone,
            'email' => $email,
        );
        $this->load->model('user/Model_User', 'user');
        $status = $this->user->modifyDetailUser($this->uInfo['passport'], $data);

        if ($status) {
            $response = array('error' => '0', 'msg' => '修改成功', 'code' => 'modify_success');;
        } else {
            $response = error(10014);
        }

        self::json_output($response);
    }

    /**
     * 我的充值记录
     */
    public function payLog()
    {
        $Limit = 20;
        $currentPage = $this->uri->segment(4, 1);//echo $currentPage;exit;
        $offset = ($currentPage - 1) * $Limit;

        $startTime = $this->input->get_post('start_time');
        $endTime = $this->input->get_post('end_time');

        $startTime = empty ($startTime) ? date('Y-m-d H:i:s', time()-(7*86400)) : date('Y-m-d H:i:s', strtotime($startTime));
        $endTime = empty ($endTime) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', strtotime($endTime));

        $this->load->model('Model_Order', 'order');
        $data = $this->order->getOrderByUid($this->uInfo['passport'], $startTime, $endTime, $Limit, $offset);
        $totalNum = $this->order->getUserOrderCount($this->uInfo['passport'], $startTime, $endTime);

        $this->load->library('pagination');
        $config['base_url'] = '/user/center/payLog/';
        $config['total_rows'] = $totalNum;
        $config['per_page'] = $Limit;
        $config['num_links'] = 10;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['suffix'] = '/?start_time='.$startTime.'&end_time='.$endTime;
        $config['anchor_class'] = 'class="page"';
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();

//var_dump($pageHtml);exit;
        $this->load->view('user/pay_log', array('current' => 'pay_log', 'navigation' => 'center', 'page_html' => $pageHtml, 'data' => $data));
    }

    /**
     * 修改密码
     */
    public function modifyPassword()
    {

        $this->load->view('user/modify_password', array('current' => 'modify_password', 'navigation' => 'center'));
    }

    /**
     * 提交修改密码
     */
    public function submitModifyPassword()
    {
        $oldPassword = $this->input->get_post('old_password');
        $newPassword = $this->input->get_post('new_password');

        $response = array('error' => '0', 'msg' => '修改成功', 'code' => 'modify_success');

        do {
            if ( empty($oldPassword) || empty ($newPassword) ) {
                $response = error(10012);
                break;
            }

            if ( !$this->isLogin() ) {
                $response = error(10009);
                break;
            }

            self::include_uc_lib();
            $ucresult = uc_user_edit(addslashes($this->uInfo['passport']), $oldPassword, $newPassword, '', 0, '', '');
            if($ucresult < 0)
            {
                $response = error(10007);
                break;
            }
        } while (false);

        self::json_output($response);
    }


}
